package com.qk.management.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 *
 * @author blusr
 * @version 1.0
 * @date 2025/10/7 19:49
 */
@Service
@SuppressWarnings("all")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> page(RoleDTO roleDto) {
        PageHelper.startPage(roleDto.getPage(),roleDto.getPageSize());
        List<Role> roles = roleMapper.list(roleDto);
        Page<Role> pageInfo = (Page<Role>)roles;
        return PageResult.<Role>builder()
                .total(pageInfo.getTotal())
                .rows(pageInfo.getResult())
                .build();
    }

    @Override
    public void addRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.addRole(role);
    }

    @Override
    public void deleteRole(Long id) {
        roleMapper.deleteRole(id);
    }

    @Override
    public Role getRole(Long id) {
        return roleMapper.getRole(id);
    }

    @Override
    public void updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }

    @Override
    public List<Role> getRoles() {
        return roleMapper.getRoles();
    }
}
